package com.auto.util;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import net.sf.json.JsonConfig;
import net.sf.json.processors.JsonValueProcessor;
import net.sf.json.processors.PropertyNameProcessor;
import net.sf.json.util.CycleDetectionStrategy;
import net.sf.json.util.PropertyFilter;

import com.auto.base.pojo.SysModule;
import com.base.common.CommonConstants;



final public class MyJsonConfig {

public static JsonConfig JSON_CFG;
	
	public static JsonConfig JSON_CFG_QUERY_MAP;//查询map
	

	static {
		//JSON_CFG_QUERY_MAP = new JsonConfig();
		//JSON_CFG_QUERY_MAP.setCycleDetectionStrategy(CycleDetectionStrategy.LENIENT);
//		JSON_CFG_QUERY_MAP.registerJsonPropertyNameProcessor(Map.class, new PropertyNameProcessor(){ 
//			@Override 
//		    public String processPropertyName(Class obj, String name) { 
//		       return  JUtil.underlineToCamel(name); 
//		    } 
//		});
		
		
		JSON_CFG = new JsonConfig();
		// 防止自包含
		JSON_CFG.setCycleDetectionStrategy(CycleDetectionStrategy.LENIENT);
		
		 
		  JSON_CFG.registerJsonPropertyNameProcessor(SysModule.class, new PropertyNameProcessor(){ 
				@Override 
			    public String processPropertyName(Class obj, String name) { 
			        if(name.equalsIgnoreCase("modulePid")){
			        	return "_parentId"; 
			        }
			       return name; 
			} 
			});
	  
	  
//	  JSON_CFG.registerJsonPropertyNameProcessor(SysModule.class, new PropertyNameProcessor(){ 
//			@Override 
//		    public String processPropertyName(Class obj, String name) { 
//		        if(name.equalsIgnoreCase("modulePid")){
//		        	return "_parentId"; 
//		        }
//		       return name; 
//		} 
//		});
//	  
//	  JSON_CFG.registerJsonPropertyNameProcessor(SysUser.class, new PropertyNameProcessor(){ 
//			@Override 
//		    public String processPropertyName(Class obj, String name) { 
//		        if(name.equalsIgnoreCase("userDept")){
//		        	return "_parentId"; 
//		        }
//		       return name; 
//		} 
//		});
	  
	  
		
		// 设置日期格式
		JSON_CFG.registerJsonValueProcessor(Date.class, new JsonValueProcessor() {
			@Override
			public Object processObjectValue(String name, Object value, JsonConfig cfg) {
				if (!(value instanceof Date))
					return null;
				DateFormat dateFormat = new SimpleDateFormat(CommonConstants.DATE_FORMAT);
				String dateStr = dateFormat.format(value);
				return dateStr;
			}

			@Override
			public Object processArrayValue(Object arg0, JsonConfig arg1) {
				return null;
			}
		});
		
		// 属性过滤空值
		    JSON_CFG.setJsonPropertyFilter(new PropertyFilter() {
			@Override
			public boolean apply(Object obj, String name, Object value) {
				return value == null;
			}
		});
		  
	}
}
